package com.gidgabs.modules.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gidgabs.common.annotation.LoginUserApi;
import com.gidgabs.common.utils.PageUtils;
import com.gidgabs.common.utils.Query;
import com.gidgabs.common.utils.R;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.modules.api.service.TbProductCommentLikeService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbProductCommentLikeEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 产品关系表-评论点赞表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-29 17:16:10
 */
@Api("产品评论点赞模块")
@RestController
@RequestMapping("/api/productcommentlike")
public class TbProductCommentLikeController {

    @Autowired
    private TbProductCommentLikeService tbProductCommentLikeService;

    @Autowired
    private TbUserService userService;

    /**
     * 评论点赞添加 or 删除评论点赞
     *
     * @param productCommentId
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "评论点赞添加 or 删除评论点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCommentId", value = "产品评论id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<TbProductCommentLikeEntity> save(Integer productCommentId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        //判断当前评论是否点赞
        TbProductCommentLikeEntity tbProductCommentLikeEntity = new TbProductCommentLikeEntity();
        tbProductCommentLikeEntity.setUserId(user.getUserId());
        tbProductCommentLikeEntity.setProductCommentId(productCommentId);
        //如果为空则未点赞 就添加点赞记录 反之则删除
        TbProductCommentLikeEntity productCommentLikeEntity = tbProductCommentLikeService.queryByObject(tbProductCommentLikeEntity);
        if (productCommentLikeEntity == null) {
            tbProductCommentLikeEntity.setCreateTime(new Date());
            tbProductCommentLikeService.save(tbProductCommentLikeEntity);
        } else {
            tbProductCommentLikeService.delete(productCommentLikeEntity.getId());
        }
        return ReturnJson.success("操作成功");
    }

}
